<?php

namespace Biz\Models\User;

use Biz\Models\Model;
use Laravel\Passport\HasApiTokens;
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Lumen\Security\PasswordSecurity;
use Biz\Constants\User\UserConstants;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Passport\PersonalAccessTokenResult;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

/**
 * User.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasApiTokens, SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * @var array
     */
    protected $fillable = [
        'type',
        'email',
        'mobile',
        'avatar',
        'nickname',
        'username',
        'password',
        'salt',
    ];

    /**
     * 个人访问token.
     *
     * @var PersonalAccessTokenResult
     */
    private $personalAccessTokenResult;

    /**
     * 客户用户.
     *
     * @var UserClient
     */
    private $userClient;

    /**
     * 成员用户.
     *
     * @var UserMember
     */
    private $userMember;

    /**
     * Set PersonalAccessTokenResult.
     *
     * @param PersonalAccessTokenResult $personalAccessTokenResult
     */
    public function setPersonalAccessTokenResult(PersonalAccessTokenResult $personalAccessTokenResult)
    {
        $this->personalAccessTokenResult = $personalAccessTokenResult;
    }

    /**
     * Get PersonalAccessTokenResult.
     *
     * @return PersonalAccessTokenResult
     */
    public function getPersonalAccessTokenResult()
    {
        return $this->personalAccessTokenResult;
    }

    /**
     * Get PersonalAccessTokenResult.
     *
     * @return string|null
     */
    public function getPersonalAccessToken()
    {
        if ($this->personalAccessTokenResult) {
            return $this->personalAccessTokenResult->accessToken;
        }

        return null;
    }

    /**
     * For Laravel Passport.
     *
     * @param $username
     *
     * @return Model|null
     */
    public function findForPassport($username)
    {
        return $this->where('mobile', $username)->first();
    }

    /**
     * For Laravel Passport.
     *
     * @param $password
     *
     * @return bool
     */
    public function validateForPassportPasswordGrant($password): bool
    {
        return PasswordSecurity::check($this->salt, $password, $this->getAuthPassword());
    }

    /**
     * 是否为业主客户.
     */
    public function isClient()
    {
        return UserConstants::TYPE_CLIENT === $this->type;
    }

    /**
     * 查询客户用户.
     */
    public function client()
    {
        return $this->hasOne(UserClient::class, 'user_id', 'id');
    }

    /**
     * 设置客户用户.
     *
     * @param UserClient $userClient
     */
    public function setClient(UserClient $userClient)
    {
        $this->userClient = $userClient;
    }

    /**
     * 获取客户用户.
     *
     * @return UserClient| null
     */
    public function getClient()
    {
        return $this->userClient;
    }

    /**
     * 是否为成员用户.
     */
    public function isMember()
    {
        return UserConstants::TYPE_MEMBER === $this->type;
    }

    /**
     * 查询成员用户.
     */
    public function member()
    {
        return $this->hasOne(UserMember::class, 'user_id', 'id');
    }

    /**
     * 设置成员用户.
     *
     * @param UserMember $userMember
     */
    public function setMember(UserMember $userMember)
    {
        $this->userMember = $userMember;
    }

    /**
     * 获取成员用户.
     *
     * @return UserMember| null
     */
    public function getMember()
    {
        return $this->userMember;
    }
}
